var express = require('express');
var router = express.Router();
var multiparty = require('multiparty');
var async = require("async");
var fs = require("fs");
var DB = require("../../model/db");

var host = "";
router.use(function (req, res, next) {
    Host = "http://" + req.headers.host;
    next();
})

router.get('/', function (req, res) {
    var page = req.query.page;
    if (!page) {
        page = 1;
    }
    async.parallel({
        count: function (callback) {
            DB.count("product", {}, function (err, data) {
                callback(err, data)
            })
        },
        product: function (callback) {
            DB.find("product", {},{title: 1, description: 1, old_price: 1, price: 1, img: 1}, { pageSize: 10, page: page }, function (err, data) {
                callback(err, data)
            })
        }
    }, function (err, data) {
        res.render("admin/product/index", {
            host: Host,
            list: data.product,
            totalPages: Math.ceil(data.count / 10),
            page:page
        })
    })
});
router.get("/add",function(req,res){
    res.render("admin/product/add",{
        host:Host
    })
})

router.post("/addPost",function(req,res){
    var form = new multiparty.Form();
    form.uploadDir = "./public/resource";
    form.parse(req, function (err, fields, files) {
        var title = fields.title[0];
        var old_price = fields.old_price[0];
        var price = fields.price[0];
        var img = files.img[0].path;
        var description = fields.description[0];
        var product_content = fields.product_content[0]
        DB.insertOne("product", { title: title, old_price: old_price, price: price, img: img, description: description, product_content: product_content }, function (err, result) {
            if (err) {
                console.log(err);
                return;
            } else {
                res.send('<script>alert("添加成功");location.href="/admin/product"</script>');
            }
        })
    })
})

router.get("/edit",function(req,res){
    var id = req.query.id;
    DB.find("product", { "_id": new DB.ObjectID(id) }, function (err, data) {
        if (err) {
            console.log(err);
            return;
        } else {
            res.render("admin/product/edit", {
                host: Host,
                list: data[0]
            })
        }
    })
})

router.post("/editPost",function(req,res){
    var form = new multiparty.Form();
    form.uploadDir = "./public/resource";
    form.parse(req, function (err, fields, files) {
        var id = fields.id[0];
        var title = fields.title[0];
        var old_price = fields.old_price[0];
        var price = fields.price[0];
        var img = files.img[0].path;
        var description = fields.description[0];
        var product_content = fields.product_content[0];
        if(files.img[0].originalFilename==""){
            var setData ={
                title: title, old_price: old_price, price: price, description: description, product_content: product_content 
            }
        }else{
             var setData ={
                title: title, old_price: old_price, price: price,img:img, description: description, product_content: product_content 
            }
        }
        DB.updateOne("product", { "_id": new DB.ObjectID(id) },setData, function (err, result) {
            if (err) {
                console.log(err);
                return;
            } else {
                res.send('<script>alert("修改成功");location.href="/admin/product"</script>');
            }
        })
    })
})

router.get("/delete/:id",function(req,res){
    var id = req.params.id;
    DB.deleteMany("product", { "_id": new DB.ObjectID(id) }, function (err, result) {
        if (err) {
            console.log(err);
            return;
        } else {
            res.redirect(Host + "/admin/product");
        }
    })
})

router.post('/upload',function(req,res){
    var form = new multiparty.Form();
    form.uploadDir='./public/resource'  /*设置图片上传的路径*/
    form.parse(req, function(err, fields, files) {
        var path=files.filedata[0].path;
        res.json({"err":"","msg":path})  /*给编辑器返回地址信息*/
    });
})

module.exports = router;